﻿/*
 * BitmapMapRendererBase
 * 
 * initial implementation : 20.11.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
//using Petra.Gis.Drawing;
using Petra.Gis.RenderingServices.PlanarBase ;


namespace Petra.Gis.RenderingServices.GDIPlus
{

  /// <summary>
  /// abstract base class for all renderers implemented for <c>Petra.Gis.RenderingServices.GDIPlus.BitmapMap</c>
  /// </summary>
  public abstract class BitmapMapRendererBase : RendererBase
  {

    protected Graphics _outGraphic;
    protected int xleft, xright, ybottom, ytop;



    /// <summary>
    /// xaml constr.
    /// </summary>
    public BitmapMapRendererBase() 
      :base(){ }





    //public BitmapMapRendererBase(BitmapMap map, RenderStyle s)
    //  : base((MapBase)map, s){ }






    public override void PrepareToRender()
    {
      // must call base
      base.PrepareToRender();

      // get output graphics
      _outGraphic = ((BitmapMap)rendererMap).OutputGraphics;


      // get map range
      xleft = -10;
      ybottom = -10;
      xright = (int)rendererMap.Width + 10;
      ytop = (int)rendererMap.Height + 10;

      //Point tmpPt = ((BitmapMap)_map).RenderTransform.ProjectionToMap(_map.ProjectionExtent.Left, _map.ProjectionExtent.Bottom);
      //xleft = tmpPt.X;
      //ybottom = tmpPt.Y;

      //tmpPt = ((BitmapMap)_map).RenderTransform.ProjectionToMap(_map.ProjectionExtent.Right, _map.ProjectionExtent.Top);
      //xright = tmpPt.X;
      //ytop = tmpPt.Y;
    }



    private Style _style;

    protected override RenderStyle renderStyle
    {
      get { return _style; }
      set { _style = (Style)value; }
    }




    new public Style Style
    {
      get { return _style; }
      set
      {
        //todo: style must contain at least few items
        if (value == null)
          throw new ArgumentException("invalid null style", "style");

        _style = value;
      }
    }




    //public Graphics OutputGraphics
    //{
    //  set
    //  {
    //    if (value == null)
    //      throw new ArgumentException("null Graphics not allowed");

    //    _outGraphic = value; 
    //  }
    //}





    //protected override MapBase renderMap
    //{
    //  get { return _map; }
    //  set { _map = (BitmapMap)value; }
    //}



    //private BitmapMap _map;

    ///// <summary>
    ///// set <c>BitmapMap</c> that renderer uses to output (render) gis data into as map layer
    ///// </summary>
    //new public BitmapMap Map
    //{
    //  set
    //  {
    //    if (value == null)
    //      throw new ArgumentException("invalid null map", "map");

    //    _map = value;

    //  }
    //}





    //int z = Convert.ToInt32("11001001", 2);
    /* for line:
     * for start point uses xxxx0000, first 4 bits
     * for end point   uses 0000xxxx, last  4 bits
     * for point only  uses xxxx0000, first 4 bits
     * 
     *         |        |
     *   0110  |  0100  |  0101
     *         |        |   
     * --------------------------
     *         |        |
     *   0010  |  0000  |  0001
     *         |        |
     * --------------------------
     *         |        |   
     *   1010  |  1000  |  1001
     *         |        |   
     */
    /*
    internal int getLineIntesectMask(Point startP, Point endP)
    {
      int xs, xe;
      int ys, ye;

      xs = (startP.X <= _Bbox.left ? 2 : 0);
      xs = (startP.X <= _Bbox.right && xs == 2 ? 2 : 1);
      ys = (startP.Y <= _Bbox.bottom ? 8 : 0);
      ys = (startP.Y <= _Bbox.top && ys == 8 ? 8 : 4);

      xe = (endP.X <= _Bbox.left ? 32 : 0);
      xe = (endP.X <= _Bbox.right && xe == 32 ? 32 : 16);
      ye = (endP.Y <= _Bbox.bottom ? 128 : 0);
      ye = (endP.Y <= _Bbox.top && ye == 128 ? 128 : 64);
      return xs | ys | xe | ye;
    }
    
    */



    protected int getPointIntesectMask(Point pnt)
    {
      int xs;
      int ys;

      xs = (pnt.X < xleft ? 2 : 0);
      xs = (pnt.X <= xright ? xs : 1);
      ys = (pnt.Y < ybottom ? 8 : 0);
      ys = (pnt.Y <= ytop ? ys : 4);

      //xs = (pnt.X < 0 ? 2 : 0);
      //xs = (pnt.X <= _map.Width ? xs : 1);
      //ys = (pnt.Y < 0 ? 8 : 0);
      //ys = (pnt.Y <= _map.Height ? ys : 4);


      //if (xs == 8 || ys == 8)
      //  return 0;

      return xs | ys;
    }

  }

}
